home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / src / plstream.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-26  |  7.9 KB  |  298 lines

  1. /* $Id: plstream.c,v 1.18 1994/07/26 18:17:24 mjl Exp $
  2.  * $Log: plstream.c,v $
  3.  * Revision 1.18  1994/07/26  18:17:24  mjl
  4.  * Added missing variable declaration and initializer.
  5.  *
  6.  * Revision 1.17  1994/07/26  09:00:46  mjl
  7.  * Added a quick fix so that hitting a carriage return when prompted for
  8.  * a filename no longer aborts the program.  Contributed by Mark Olesen.
  9.  *
  10.  * Revision 1.16  1994/06/30  18:22:17  mjl
  11.  * All core source files: made another pass to eliminate warnings when using
  12.  * gcc -Wall.  Lots of cleaning up: got rid of includes of math.h or string.h
  13.  * (now included by plplot.h), and other minor changes.  Now each file has
  14.  * global access to the plstream pointer via extern; many accessor functions
  15.  * eliminated as a result.
  16.  *
  17.  * Revision 1.15  1994/05/10  21:52:45  mjl
  18.  * Put in a slight optimization in cmap1 color interpolation.
  19.  *
  20.  * Revision 1.14  1994/04/30  16:15:13  mjl
  21.  * Fixed format field (%ld instead of %d) or introduced casts where
  22.  * appropriate to eliminate warnings given by gcc -Wall.
  23.  *
  24.  * Revision 1.13  1994/03/23  08:32:34  mjl
  25.  * Moved color and color map handling functions into plctrl.c.
  26.  * Changed file open routine to support new options for sequencing family
  27.  * member files.
  28.  *
  29.  * Revision 1.12  1994/01/15  17:28:59  mjl
  30.  * Added include of pdf.h.
  31. */
  32.  
  33. /*    plstream.c
  34.  
  35.     Stream & device support functions.
  36. */
  37.  
  38. #include "plplotP.h"
  39.  
  40. /*----------------------------------------------------------------------*\
  41.  * plcol_interp()
  42.  *
  43.  * Initializes device cmap 1 entry by interpolation from pls->cmap1
  44.  * entries.  Returned PLColor is supposed to represent the i_th color
  45.  * out of a total of ncol colors in the current color scheme.
  46. \*----------------------------------------------------------------------*/
  47.  
  48. void
  49. plcol_interp(PLStream *pls, PLColor *newcolor, int i, int ncol)
  50. {
  51.     float x, delta;
  52.     int il, ir;
  53.  
  54.     x = (double) (i * (pls->ncol1-1)) / (double) (ncol-1);
  55.     il = x;
  56.     ir = il + 1;
  57.     delta = x - il;
  58.  
  59.     if (ir > pls->ncol1 || il < 0)
  60.     fprintf(stderr, "Invalid color\n");
  61.  
  62.     else if (ir == pls->ncol1 || (delta == 0.)) {
  63.     newcolor->r = pls->cmap1[il].r;
  64.     newcolor->g = pls->cmap1[il].g;
  65.     newcolor->b = pls->cmap1[il].b;
  66.     }
  67.     else {
  68.     newcolor->r = (1.-delta) * pls->cmap1[il].r + delta * pls->cmap1[ir].r;
  69.     newcolor->g = (1.-delta) * pls->cmap1[il].g + delta * pls->cmap1[ir].g;
  70.     newcolor->b = (1.-delta) * pls->cmap1[il].b + delta * pls->cmap1[ir].b;
  71.     }
  72. }
  73.  
  74. /*----------------------------------------------------------------------*\
  75.  * plOpenFile()
  76.  *
  77.  * Opens file for output, prompting if not set.
  78.  * Prints extra newline at end to make output look better in batch runs.
  79.  * A file name of "-" indicates output to stdout.
  80. \*----------------------------------------------------------------------*/
  81.  
  82. #define MAX_NUM_TRIES    10
  83. void
  84. plOpenFile(PLStream *pls)
  85. {
  86.     int i = 0, count = 0;
  87.     size_t len;
  88.     char line[256];
  89. #ifdef __riscos
  90.     char ro_Filename[11]; /* riscos file length is brain damaged */
  91. #endif
  92.     while (pls->OutFile == NULL) {
  93.  
  94. /* Setting pls->FileName = NULL forces creation of a new family member */
  95. /* You should also free the memory associated with it if you do this */
  96.  
  97.     if (pls->family && pls->BaseName != NULL)
  98.         plP_getmember(pls);
  99.  
  100. /* Prompt if filename still not known */
  101.  
  102.     if (pls->FileName == NULL) {
  103.         do {
  104.         printf("Enter desired name for graphics output file: ");
  105.         fgets(line, sizeof(line), stdin);
  106.         len = strlen(line);
  107.         if (len)
  108.             len--;
  109.         line[len] = '\0';    /* strip new-line */
  110.         count++;        /* count zero entries */
  111.         } while (!len && count < MAX_NUM_TRIES);
  112.         plP_sfnam(pls, line);
  113.     }
  114.  
  115. /* If name is "-", send to stdout */
  116.  
  117.     if ( ! strcmp(pls->FileName, "-")) {
  118.         pls->OutFile = stdout;
  119.         pls->output_type = 1;
  120.         break;
  121.     }
  122.  
  123. /* Need this here again, for prompted family initialization */
  124.  
  125.     if (pls->family && pls->BaseName != NULL)
  126.         plP_getmember(pls);
  127.  
  128.     if (i++ > 10)
  129.         plexit("Too many tries.");
  130. #ifdef __riscos /* fix RISC OS filename brain damage :-( */
  131.         strncpy(ro_Filename, pls->FileName, 10);
  132.         ro_Filename[10]='\0';
  133.         strcpy (pls->FileName, ro_Filename);
  134. #endif
  135.  
  136.     if ((pls->OutFile = fopen(pls->FileName, "wb+")) == NULL)
  137.         printf("Can't open %s.\n", pls->FileName);
  138.     else
  139.         fprintf(stderr, "Created %s\n", pls->FileName);
  140.     }
  141. }
  142.  
  143. /*----------------------------------------------------------------------*\
  144.  * plP_getmember()
  145.  *
  146.  * Sets up next file member name (in pls->FileName), but does not open it.
  147. \*----------------------------------------------------------------------*/
  148.  
  149. void
  150. plP_getmember(PLStream *pls)
  151. {
  152.     char tmp[256];
  153.  
  154.     if (pls->FileName == NULL)
  155.     pls->FileName = (char *) malloc(10 + strlen(pls->BaseName));
  156.  
  157. #ifdef __riscos
  158.     sprintf(tmp, "%s_%%0%1ii", pls->BaseName, (int) pls->fflen);
  159. #else
  160.     sprintf(tmp, "%s.%%0%1ii", pls->BaseName, (int) pls->fflen);
  161. #endif
  162.     sprintf(pls->FileName, tmp, pls->member);
  163. }
  164.  
  165. /*----------------------------------------------------------------------*\
  166.  * plP_sfnam()
  167.  *
  168.  * Sets up file name & family stem name.
  169.  * Reserve some extra space (5 chars) to hold an optional member number.
  170. \*----------------------------------------------------------------------*/
  171.  
  172. void
  173. plP_sfnam(PLStream *pls, const char *fnam)
  174. {
  175.     pls->OutFile = NULL;
  176.  
  177.     if (pls->FileName != NULL)
  178.     free((void *) pls->FileName);
  179.  
  180.     pls->FileName = (char *) malloc(10 + strlen(fnam));
  181.  
  182.     strcpy(pls->FileName, fnam);
  183.  
  184.     if (pls->BaseName != NULL)
  185.     free((void *) pls->BaseName);
  186.  
  187.     pls->BaseName = (char *) malloc(10 + strlen(fnam));
  188.  
  189.     strcpy(pls->BaseName, fnam);
  190. }
  191.  
  192. /*----------------------------------------------------------------------*\
  193.  * plFamInit()
  194.  *
  195.  * Initializes family file parameters.
  196. \*----------------------------------------------------------------------*/
  197.  
  198. void
  199. plFamInit(PLStream *pls)
  200. {
  201.     if (pls->family) {
  202.     pls->bytecnt = 0;
  203.     if ( ! pls->member)
  204.         pls->member = 1;
  205.     if ( ! pls->finc)
  206.         pls->finc = 1;
  207.     if ( ! pls->fflen)
  208.         pls->fflen = 1;
  209.     if ( ! pls->bytemax)
  210.         pls->bytemax = PL_FILESIZE_KB * 1000;
  211.     }
  212. }
  213.  
  214. /*----------------------------------------------------------------------*\
  215.  * plGetFam()
  216.  *
  217.  * Starts new member file of family file set if necessary.
  218.  *
  219.  * Note each member file is a complete graphics file (can be printed
  220.  * individually), although 'plrender' will treat a family as a single
  221.  * logical file if given the family name instead of the member name.
  222. \*----------------------------------------------------------------------*/
  223.  
  224. void
  225. plGetFam(PLStream *pls)
  226. {
  227.     if (pls->family) {
  228.     if (pls->bytecnt > pls->bytemax || pls->famadv) {
  229.         plP_tidy();
  230.         pls->member += pls->finc;
  231.         pls->famadv = 0;
  232.         plP_init();
  233.         return;
  234.     }
  235.     }
  236. }
  237.  
  238. /*----------------------------------------------------------------------*\
  239.  * plRotPhy()
  240.  *
  241.  * Rotates physical coordinates if necessary for given orientation.
  242.  * Each time orient is incremented, the plot is rotated 90 deg clockwise.
  243.  * Note: this is now used only to rotate by 90 degrees for devices that
  244.  * expect portrait mode.
  245. \*----------------------------------------------------------------------*/
  246.  
  247. void
  248. plRotPhy(PLINT orient, PLINT xmin, PLINT ymin, PLINT xmax, PLINT ymax,
  249.      int *px, int *py)
  250. {
  251.     int x, y;
  252.  
  253.     x = *px;
  254.     y = *py;
  255.  
  256.     switch (orient%4) {
  257.  
  258.     case 1:
  259.     *px = xmin + (y - ymin);
  260.     *py = ymin + (xmax - x);
  261.     break;
  262.  
  263.     case 2:
  264.     *px = xmin + (xmax - x);
  265.     *py = ymin + (ymax - y);
  266.     break;
  267.  
  268.     case 3:
  269.     *px = xmin + (ymax - y);
  270.     *py = ymin + (x - xmin);
  271.     break;
  272.  
  273.     default:
  274.     break;            /* do nothing */
  275.     }
  276. }
  277.  
  278. /*----------------------------------------------------------------------*\
  279.  * plAllocDev()
  280.  *
  281.  * Allocates a standard PLDev structure for device-specific data, stores
  282.  * the address in pls->dev, and returns the address as well.
  283. \*----------------------------------------------------------------------*/
  284.  
  285. PLDev *
  286. plAllocDev(PLStream *pls)
  287. {
  288.     if (pls->dev != NULL)
  289.     free((void *) pls->dev);
  290.  
  291.     pls->dev = calloc(1, (size_t) sizeof(PLDev));
  292.     if (pls->dev == NULL)
  293.     plexit("plAllocDev: cannot allocate memory\n");
  294.  
  295.     return (PLDev *) pls->dev;
  296. }
  297.  
  298.